package com.gywj.race.controller.api;

import com.google.common.collect.Maps;
import com.gywj.race.config.result.AjaxResultHandler;
import com.gywj.race.dto.OssDeleteFileDto;
import com.gywj.race.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 管理端Oss文件存储API控制类
 *
 * @author: CCG
 * @since: 2018 /11/29
 * @version: v1.0.0
 */
@Api(description = "管理端Oss文件存储API控制类")
@RestController("oss/api")
public class OssApiController {

    @Resource
    private OssService ossService;

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS},
            origins = "*",
            allowedHeaders = {"Origin", "X-Requested-With", "x_requested_with", "Content-Type", "Accept"})
    @PostMapping("uploadFile")
    @ResponseBody
    public Map<String, Object> uploadFile(@RequestBody MultipartFile upload) {
        try {
            String url = ossService.uploadFile(upload);
            Map<String, Object> result = Maps.newHashMap();
            result.put("uploaded", 1);
            result.put("fileName", upload.getName());
            result.put("url", url);
            return result;

        } catch (Exception e) {
            Map<String, Object> result = Maps.newHashMap();
            result.put("uploaded", 0);
            Map<String, Object> errorMap = Maps.newHashMap();
            errorMap.put("message", e.getMessage());
            result.put("error", errorMap);
            return result;
        }
    }

    @ApiOperation(value = "上传文件自定义文件名", notes = "上传文件自定义文件名")
    @PostMapping("uploadFileWithFileName")
    @ResponseBody
    public Map<String, Object> uploadFileWithFileName(@RequestBody MultipartFile file, @RequestParam String fileName) {
        try {
            String url = ossService.uploadFileWithFileName(file, fileName);
            return AjaxResultHandler.Success("AjaxResultHandler.Success", url);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    @ApiOperation(value = "上传文件base64", notes = "上传文件base64")
    @PostMapping("putDataBase64Jpg")
    @ResponseBody
    public Map<String, Object> putDataBase64Jpg(@RequestBody Map map) {
        try {
            String content = (String) map.get("content");
            String url = ossService.putDataBase64Jpg(content);
            return AjaxResultHandler.Success("AjaxResultHandler.Success", url);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }




    @ApiOperation(value = "上传压缩图片", notes = "上传压缩图片")
    @PostMapping("uploadPicWithCompress")
    @ResponseBody
    public Map<String, Object> uploadPicWithCompress(@RequestBody MultipartFile upload) {
        try {
            String url = ossService.uploadPicWithCompress(upload);
            Map<String, Object> result = Maps.newHashMap();
            result.put("uploaded", 1);
            result.put("fileName", upload.getName());
            result.put("url", url);
            return result;

        } catch (Exception e) {
            Map<String, Object> result = Maps.newHashMap();
            result.put("uploaded", 0);
            Map<String, Object> errorMap = Maps.newHashMap();
            errorMap.put("message", e.getMessage());
            result.put("error", errorMap);
            return result;
        }
    }

    @ApiOperation(value = "更新文件", notes = "更新文件")
    @PostMapping("updateFile")
    @ResponseBody
    public Map<String, Object> updateFile(@RequestBody MultipartFile file, @RequestParam("oldUrl") String oldUrl) {
        try {
            String url = ossService.updateFile(file, oldUrl);
            return AjaxResultHandler.Success("AjaxResultHandler.Success", url);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @ApiOperation(value = "更新压缩图片", notes = "更新压缩图片")
    @PostMapping("updatePicWithCompress")
    @ResponseBody
    public Map<String, Object> updatePicWithCompress(@RequestBody MultipartFile file, @RequestParam("oldUrl") String oldUrl) {
        try {
            String url = ossService.updatePicWithCompress(file, oldUrl);
            return AjaxResultHandler.Success("AjaxResultHandler.Success", url);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @ApiOperation(value = "根据文件url列表批量删除文件", notes = "根据文件url列表批量删除文件")
    @PostMapping("deleteFile")
    @ResponseBody
    public Map<String, Object> deleteFile(@RequestBody OssDeleteFileDto ossDeleteFileDto) {
        try {
            Integer count = ossService.deleteFile(ossDeleteFileDto.getUrls());
            return AjaxResultHandler.Success("AjaxResultHandler.Success", count);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }
}
